package com.yc.upload;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

@Configuration
@ComponentScan("com.yc.upload")
// 判断类路径中是否包含 SpringMVC 分发器Servlet
@ConditionalOnClass(DispatcherServlet.class)
@ConditionalOnProperty(prefix = "yc.upload", value = {"diskpath", "webpath"})
public class UploadApp implements WebMvcConfigurer {

    @Resource
    private UploadProperties uploadProperties;

    // 静态资源映射
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String webpath = uploadProperties.getWebpath();
        String diskpath = uploadProperties.getDiskpath();
        // /upload/**
        registry.addResourceHandler(webpath + "**")
                // file:/d:/a/b/
                .addResourceLocations("file:/" + diskpath);
    }
}
